cd "${mystart}/Simulated data\Calibration Minorities/Benchmark Data Minorities/"


* Table 123 
* wage distribution
qui{
clear
use "Data files/Benchmark.dta"
sort ID age
keep if age<65
replace wage_noise=. if  wage_noise<3.5  //(min wage)
keep if employed_yn==1
rename wage_noise incwage_hourly
keep if age>=40 & age<=50
	
collapse (p5) p5=incwage_hourly (p25) p25=incwage_hourly (p50) p50=incwage_hourly (p75) p75=incwage_hourly (p90) p90=incwage_hourly (p95) p95=incwage_hourly (p99) p99=incwage_hourly , by (race)	
gen data=1 // 1= simulation. 0=real data
append using "${mystart}\CPS\CPS Data Files\Intermediate Data Files\Wage_dist2_races.dta"
sort  race  data

reshape long p, i(race data) j(percentile)
reshape wide p, i(race percentile) j(data)
reshape wide p0 p1, i(percentile) j(race )

label var p02 "CPS, Blacks"
label var p12 "Model, Blacks"
label var p03 "CPS, Hispanics"
label var p13 "Model, Hispanics"

table percentile,  statistic(mean p02 p12 p03 p13) nototals nformat(%5.1f) 
collect title "Percentiles of the wage distribution, ages 40-50, CPS Data and Model"
collect export "${out_figures}/Wage_percentiles_races.tex", tableonly replace
}


* Table 124
* compare with PSID FE regression wages
qui{
clear
use "Data files/Benchmark.dta"
sort ID age
tabstat wage_noise, stat(mean p50 p90 p95 p99 max)
replace wage_noise=. if  wage_noise<3.5  //(min wage)
replace wage_noise=. if  wage_noise>90
gen log_real_av_wage= log(wage_noise)  if emp_ft_pt==2 // FT wages only
gen log_real_av_wage_all= log(wage_noise)  // all
rename ID id

	gen I_work_lagged = 0 if emp_ft_pt[_n-1]!=2 & id==id[_n-1] & age==age[_n-1]+1
	replace I_work_lagged = 1 if emp_ft_pt[_n-1]==2 & id==id[_n-1] & age==age[_n-1]+1
	replace I_work_lagged = 2 if emp_ft_pt[_n-1]==1 & id==id[_n-1] & age==age[_n-1]+1 
	
	gen exp_full=hours/40
	 replace exp_full=exp_full[_n-1]+exp_full if id==id[_n-1]
	 gen exp_full_sq=exp_full^2
	 
	 keep if age>=30 & age<=55
	 
gen NE_lagged1 = 0 if I_work_lagged == 1 | I_work_lagged == 2
replace NE_lagged1 = 1 if I_work_lagged == 0

label var NE_lagged1 "Lagged Non-employed"
label define lab_ne_lagged 0 "Employed Last Yr" 1 "Not Employed Last Yr"
label values NE_lagged1 lab_ne_lagged

egen x=count(log_real_av_wage_all), by(id)
drop if x<6
drop x

xtset id age

* by educ
xtreg log_real_av_wage_all age age_sq  if race==2, fe   
predict individual_effect1 if race==2, u

xtreg log_real_av_wage_all age age_sq  if race==3, fe  
predict individual_effect2 if race==3, u


gen individual_effect = individual_effect1 if race==2
replace individual_effect = individual_effect2 if race==3

collapse (max) individual_effect race, by(id)

table race,   stat(p25 individual_effect) stat(p50 individual_effect) stat(p75 individual_effect) nototal nformat(%5.2f) 
collect style header race, title(hide)
collect label levels result p25 "25th", modify
collect label levels result p50 "50th", modify
collect label levels result p75 "75th", modify
collect title "Percentiles of Individual Fixed Effects, Wage regression, Model, Ages 30-55"
collect export "${out_tables}/FE_distribution_races.tex", tableonly replace
}


* Table 127
* H transitions by insurance
qui{
clear
use "Data files/Benchmark.dta"
append using "${mystart}/Simulated data\Calibration/Benchmark Data/Data files/Benchmark.dta"
replace race=1 if race==.
label define race_lbl_new 1 "Whites" 2 "Blacks" 3 "Hispanics"
label values race  race_lbl_new

keep if education==1
keep if age<65
keep if TR==0
sort race ID age
gen H_trans=1 if (H==3 ) & H[_n+1]==3  & ID==ID[_n+1]   // stay in good  
replace H_trans=0 if H==3 & (H[_n+1]==2 | H[_n+1]==1)   & ID==ID[_n+1]  // transition to worse health

label var H_trans "Keep Good H"	
		
		logit H_trans i.race age age_sq age_cub  i.dp_record i.du_record if  INSURANCE!=1 
 eststo margin: margins i.race, at(age=45  age_sq=2025 age_cub=91125  ) atmeans post
 est sto h1
	logit H_trans i.race age age_sq age_cub  i.dp_record i.du_record if  INSURANCE==1 
 eststo margin:	 margins i.race, at(age=45  age_sq=2025 age_cub=91125  ) atmeans post
 est sto h2
	esttab h2  h1  using "${out_tables}/H_trans_race_HS_h.tex", varwidth(25) nogaps   compress label replace  ///
	 nonumbers mtitles("Has ESHI"  "No ESHI" ) nostar ///
	se   b(3) 
	eststo clear	
	
}

* Table 128
qui{
clear
use "Data files/Benchmark.dta"
append using "${mystart}/Simulated data\Calibration/Benchmark Data/Data files/Benchmark.dta"

replace race=1 if race==.

label define race_lbl_new 1 "Whites" 2 "Blacks" 3 "Hispanics"
label values race  race_lbl_new


keep if education==1
keep if age<65
keep if TR==0 & ESHI==0
keep if s_record==1 | du_record==1 | dp_record==1

tabstat I_treat
gen I_negligible_MC=1 if MC<500
replace I_negligible_MC=0 if MC>=500

replace OOP=0 if I_treat == 0 | I_pay == 0
bysort race: egen tot_OOP= total(OOP) if MC>=500
bysort race: egen tot_MC= total(MC*.6) if MC>=500
gen ratio=tot_OOP/tot_MC

replace I_pay=. if MC<500

* TABLE WITH 3 THINGS, LIMITING TO THE COMPLETELY UNINSURED WITH REPORTED SHOCK
* 1. % WITH MC<$500
* 2. % WITH OOP<.6*MC IF MC>500
* 3. mean(OOP)/mean(MC) IF MC>500 	
tabstat I_negligible_MC  // note this is the same as tabstat I_treat
tabstat I_pay  //if MC>=500 // we want the fraction not paying, so 1 minus this. 
tabstat ratio 
label var ratio "OOP/Charges if treat"
label var I_treat "% treat"
label var I_pay "% pay if treat"

* Table 128
gen All=1
table  race ,  statistic(mean I_treat) statistic(mean I_pay) statistic(mean ratio)  nototals nformat(%5.2f)  
collect title "Model"
collect style header race, title(hide)
collect export "${out_tables}/MC_stats_races.tex", tableonly replace
}

* Table 129
qui{	
clear
use "Data files/Benchmark.dta"
append using "${mystart}/Simulated data\Calibration/Benchmark Data/Data files/Benchmark.dta"
replace race=1 if race==.

label define race_lbl_new 1 "Whites" 2 "Blacks" 3 "Hispanics"
label values race  race_lbl_new


keep if education==1
keep if age<65
keep if TR==0 & ESHI==0
keep if s_record==1 | du_record==1 | dp_record==1

gen I_negligible_MC=1 if MC<500
replace I_negligible_MC=0 if MC>=500

replace OOP=0 if I_treat == 0 | I_pay == 0
bysort race H: egen tot_OOP= total(OOP) if MC>=500
bysort race H: egen tot_MC= total(MC*.6) if MC>=500
gen ratio=tot_OOP/tot_MC

replace I_pay=. if MC<500

* TABLE WITH 3 THINGS, LIMITING TO THE COMPLETELY UNINSURED WITH REPORTED SHOCK
* 1. % WITH MC<$500
* 2. % WITH OOP<.6*MC IF MC>500
* 3. mean(OOP)/mean(MC) IF MC>500 	

label var ratio "OOP/Charges if treat"
label var I_treat "% treat"
label var I_pay "% pay if treat"

gen All=1
table  (race H) ,  statistic(mean I_treat) statistic(mean I_pay) statistic(mean ratio)  nototals nformat(%5.2f)  
collect title "Model"
collect style header race, title(hide)
collect export "${out_tables}/MC_stats_H_races.tex", tableonly replace


}

